<!--
 * @Description: 三级渠道信息编辑
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-04-14 21:31:58
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-04-20 22:52:23
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="700px">
    <el-form ref="form" :model="form" label-position="left" label-width="90px" class="small-space" style="width: 90%; margin-left:0px;">
      <el-row>
        <el-col :span="11">
          <el-form-item label="父级渠道">
            <treeselect v-model="form.parent_id" :options="secondChannels" :flatten-search-results="true" :disable-branch-nodes="true" :show-count="true" placeholder="请选择父级渠道" />
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="2">
          <el-form-item label="渠道编码">
            <span v-text="form.code" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="渠道名称">
        <el-input v-model="form.name" placeholder="请填写渠道名称" />
      </el-form-item>
      <el-form-item label="测评链接">
        <el-input v-model="form.qn_url" placeholder="请填写渠道描述" />
      </el-form-item>
      <el-form-item label="渠道描述">
        <el-input v-model="form.description" type="textarea" placeholder="请填写渠道描述" />
      </el-form-item>
      <el-form-item label="链接文案">
        <el-input v-model="form.redirect_config.text" placeholder="请填写链接文案" />
      </el-form-item>
      <el-form-item label="二维码类型">
        <el-radio-group v-model="form.redirect_type">
          <el-radio label="1">H5-需要手机验证</el-radio>
          <el-radio label="2">小程序</el-radio>
          <el-radio label="3">H5-不需要手机验证</el-radio>
        </el-radio-group>
      </el-form-item>
      <div v-if="form.redirect_type == 2">
        <el-form-item label="小程序APPID">
          <el-input v-model="form.redirect_config.appid" placeholder="请填写小程序APPID" />
        </el-form-item>
        <el-form-item label="小程序路径">
          <el-input v-model="form.redirect_config.path" placeholder="请填写小程序APPID" />
        </el-form-item>
      </div>
      <div v-else>
        <el-form-item label="微信APPID">
          <el-input v-model="form.redirect_config.appid" placeholder="请填写微信APPID" />
        </el-form-item>
      </div>
      <el-form-item label="回复消息">
        <el-input key="1" :rows="6" v-model="form.subscribe_msg" type="textarea" placeholder="请填写回复消息" />
      </el-form-item>
      <el-form-item label="高级配置" class="custom-form-content">
        <json-editor key="4" v-model="form.config" />
      </el-form-item>
      <el-form-item v-if="!form.id" label="测评配置">
        <el-input key="2" :rows="2" v-model="form.testing_config" type="textarea" placeholder="请填写渠道配置" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button :loading="formSubmiting" type="primary" @click="doSubmit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import jsonEditor from '@/views/components/jsonEditor'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { jsonEditor, Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      formSubmiting: false,
      isJsonC: false,
      itemChannelModel: [],
      form: {
        id: null,
        code: null,
        name: null,
        level: 1,
        parent_id: null,
        qn_url: '',
        wechat_info: null,
        status: '1',
        description: '',
        subscribe_msg: '',
        redirect_type: '',
        redirect_config: {
          appid: '',
          path: '',
          text: ''
        },
        config: '',
        testing_config: '',
      },
      secondChannels: [],
      jsonEditor: false,
      activeSonTabName: 'other',
      defaultConfig: {
        'bg_img': 'upload/jz/backend/meta/five_dimension_bg.jpg',
        'generate_base': 'upload/jz/backend/qrcode/five_dimension/',
        'alias_config': {
          'coordinate_x': 215,
          'coordinate_y': 550,
          'dst_width': 360,
          'dst_height': 360
        },
        'description': '华米渠道用户'
      },
      wechatInfoConfig: {
        'gh_info': {
          'appid': '',
          'appsecret': ''
        },
        'wxa_info': {
          'appid': '',
          'appsecret': ''
        }
      },
      defaulttestconfig: {
        'show_pop': 1,
        'redirect_index': 1
      },
    }
  },
  mounted() {
    this.form.code = this._generateCode()
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    getCascadeSecond() {
      //查询渠道信息
      this.$api.enterprise.channel
        .cascadeSecond({
          params: { status: 1, value: 'id' }
        })
        .then(res => {
          this.secondChannels = res.data.channels
        }).catch(err => {
          console.log(err)
        })
    },
    doSubmit() {
      this.loading = true
      if (typeof this.form.wechat_info === 'object') {
        this.form.wechat_info = JSON.stringify(this.form.wechat_info)
      }
      if (typeof this.form.config === 'object') {
        this.form.config = JSON.stringify(this.form.config)
      }
      if (typeof this.form.redirect_config === 'object') {
        this.form.redirect_config = JSON.stringify(this.form.redirect_config)
      }
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$api.enterprise.channel
        .add({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    doEdit() {
      this.$api.enterprise.channel
        .edit({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    _generateCode() {
      return (new Date()).getTime().toString(16).toUpperCase()
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        code: this._generateCode(),
        name: null,
        level: 1,
        parent_id: null,
        qn_url: '',
        wechat_info: null,
        status: '1',
        description: '',
        subscribe_msg: '',
        redirect_type: '',
        redirect_config: {
          appid: '',
          path: '',
          text: ''
        },
        config: '',
        testing_config: '',
      }
    }
  }
}
</script>

<style>
.custom-form-content .el-form-item__content {
  line-height: 22px;
}
</style>
